package com.yangtzeu.jwc.activity;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yangtzeu.jwc.businessservice.BusinessHttpCallBack;
import com.yangtzeu.jwc.businessservice.impl.StudentServiceImpl;
import com.yangtzeu.jwc.customview.CustomeProgressDialog;
import com.yangtzeu.jwc.domain.Course;
import com.yangtzeu.jwc.utils.ConfirmDialog;
import com.zhangdi.jwc.R;
import com.zjsx.lib.utils.T;

public class SelectedCourseActivity extends BaseActivity {
    private ListView listView;
    private RelativeLayout nullTips;
    private ProgressDialog dialog;
    private String resultMsg;
    private BaseAdapter adapter;
    private Course deleteCource;
    List<Course> courses;
    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                dialog = CustomeProgressDialog.show(SelectedCourseActivity.this, "正在删除");
            } else {
                dialog.dismiss();
            }

            switch (msg.what) {
                case 3:
                    Toast.makeText(SelectedCourseActivity.this, "删除成功!", Toast.LENGTH_LONG).show();
                    courses.remove(deleteCource);
                    adapter.notifyDataSetChanged();
                    break;

                default:
                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setView() {
        super.setView();
        setContentView(R.layout.activity_selected_course);
    }

    @Override
    public void initActionBar() {
        super.initActionBar();
        setSimpleActionBar(null, "已选课程", null, null);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void initView() {
        super.initView();
        nullTips = (RelativeLayout) findViewById(R.id.null_tips);
        listView = (ListView) findViewById(R.id.selectedCourseList);

        Intent intent = getIntent();
        courses = (List<Course>) intent.getSerializableExtra("courses");
        if (courses.size() == 0) {
            listView.setVisibility(View.GONE);
            nullTips.setVisibility(View.VISIBLE);
        } else {
            listView.setVisibility(View.VISIBLE);
            nullTips.setVisibility(View.GONE);
            String year = intent.getStringExtra("year");
            String term = intent.getStringExtra("term");
            this.setTitle(year + term + "已选课程");
        }
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new SelectedCourseAdapter(getApplicationContext(), courses, listView, R.layout.selectedcourse_item);
        listView.setAdapter(adapter);
    }

    private final class SelectedCourseAdapter extends BaseAdapter {
        private int resource;
        private LayoutInflater inflater;
        List<Course> courses;

        public SelectedCourseAdapter(Context context, List<Course> courses, View listView, int resource) {
            super();
            this.courses = courses;
            this.resource = resource;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return courses.size();
        }

        @Override
        public Object getItem(int position) {
            return this.courses.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(resource, null);
            }
            // 得到相应条目的控件
            TextView cname = (TextView) convertView.findViewById(R.id.cname);
            TextView ccredit = (TextView) convertView.findViewById(R.id.ccredit);
            TextView clocation = (TextView) convertView.findViewById(R.id.clocation);
            TextView cteacher = (TextView) convertView.findViewById(R.id.cteacher);
            Button delete = (Button) convertView.findViewById(R.id.selected_delete);
            final Course course = (Course) getItem(position);
            cname.setText(course.getCname());
            ccredit.setText(course.getCredit());
            clocation.setText(course.getTime());
            cteacher.setText(course.getTeacher());
            if (course.isCanSelect()) {
                delete.setEnabled(true);
                delete.setVisibility(View.VISIBLE);
                delete.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (ConfirmDialog.show(SelectedCourseActivity.this, "提示", "确认删除该选课吗")) {
                            deleteCource = course;
                            deleteCource(deleteCource.getId());
                        }
                    }
                });
            } else {
                delete.setEnabled(false);
                delete.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

    }

    private void deleteCource(String courseId) {
        myHandler.sendEmptyMessage(1);
        StudentServiceImpl ssi = StudentServiceImpl.getInstance();
        ssi.qxxxGxk(MainActivity.student, courseId, new BusinessHttpCallBack() {

            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                myHandler.sendEmptyMessage(2);
                T.showLong(SelectedCourseActivity.this, failuremsg);
            }

            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, Object obj) {
                myHandler.sendEmptyMessage(3);
            }

        });
    }
}
